package com.example.androiduidemo2;// UpdatePasswordActivity.java

// ... 其他代码 ...

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androiduidemo2.database.MySQLDataHelper;


public class UpdatePasswordActivity extends AppCompatActivity {

    private EditText etStudentId;
    private EditText etOldPassword;
    private EditText etNewPassword;
    private EditText etConfirmPassword;
    private Button btnSureUpdate;
    private Button btnCancelUpdate;
    private MySQLDataHelper mySQLDataHelper4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updatepsw);

        etStudentId = findViewById(R.id.studentid);
        etOldPassword = findViewById(R.id.oldPassword);
        etNewPassword = findViewById(R.id.newPassword);
        etConfirmPassword = findViewById(R.id.confirmPassword);
        btnSureUpdate = findViewById(R.id.sureUpdate);
        btnCancelUpdate = findViewById(R.id.cancelUpdate);
        mySQLDataHelper4 = new MySQLDataHelper(this);

        // 设置确认修改按钮的点击事件
        btnSureUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studentId = etStudentId.getText().toString().trim();
                String oldPassword = etOldPassword.getText().toString().trim();
                String newPassword = etNewPassword.getText().toString().trim();
                String confirmNewPassword = etConfirmPassword.getText().toString().trim();

                // 验证输入
                if (newPassword.isEmpty() || confirmNewPassword.isEmpty() || oldPassword.isEmpty()) {
                    Toast.makeText(UpdatePasswordActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!newPassword.equals(confirmNewPassword)) {
                    Toast.makeText(UpdatePasswordActivity.this, "新密码和确认密码不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 调用更新密码的方法
                updatePassword(studentId, oldPassword, newPassword);
            }
        });

        // 设置取消修改按钮的点击事件
        btnCancelUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 直接结束当前活动
                finish();
            }
        });
    }

    private void updatePassword(String studentId, String oldPassword, String newPassword) {
        // 检查旧密码是否正确
        if (mySQLDataHelper4.login(studentId, oldPassword)) {
            // 旧密码正确，更新新密码
            boolean isUpdated = mySQLDataHelper4.updatePassword(studentId, newPassword);
            if (isUpdated) {
                Toast.makeText(UpdatePasswordActivity.this, "密码更新成功！", Toast.LENGTH_SHORT).show();
                finish(); // 关闭当前活动
            } else {
                Toast.makeText(UpdatePasswordActivity.this, "密码更新失败！", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(UpdatePasswordActivity.this, "原密码错误！", Toast.LENGTH_SHORT).show();
        }
    }
}